package com.yay.easter;

/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import com.yay.easter.graphics.EllipseRenderer;

import android.content.Context;
import android.opengl.GLSurfaceView;
import android.view.MotionEvent;

/**
 * Wrapper activity demonstrating the use of {@link GLSurfaceView}, a view
 * that uses OpenGL drawing into a dedicated surface.
 *
 * Shows:
 * + How to redraw in response to user input.
 */
public class TouchSurfaceView extends GLSurfaceView {

	
    public TouchSurfaceView(Context context) {
        super(context);
        // Turn on error-checking and logging
        setDebugFlags(DEBUG_CHECK_GL_ERROR | DEBUG_LOG_GL_CALLS);
        // We want an 8888 pixel format because that's required for
        // a translucent window.
        // And we want a depth buffer.
        //setEGLConfigChooser(8, 8, 8, 8, 16, 0);

        //mRenderer = new EggRenderer(context, false);
        mRenderer = new EllipseRenderer(context, false);
        setRenderer(mRenderer);
        //setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
        
        // Use a surface format with an Alpha channel:
        //getHolder().setFormat(PixelFormat.TRANSLUCENT);  
    }

    @Override 
    public boolean onTrackballEvent(MotionEvent e) {
        mRenderer.mAngleX += e.getX() * TRACKBALL_SCALE_FACTOR;
        mRenderer.mAngleY += e.getY() * TRACKBALL_SCALE_FACTOR;
        requestRender();
        return true;
    }

    @Override 
    public boolean onTouchEvent(final MotionEvent e) {
        float x = e.getX();
        float y = e.getY();
        switch (e.getAction()) {
        case MotionEvent.ACTION_MOVE:
            float dx = x - mPreviousX;
            float dy = y - mPreviousY;
            mRenderer.mAngleX += dx * TOUCH_SCALE_FACTOR;
            mRenderer.mAngleY += dy * TOUCH_SCALE_FACTOR;
            requestRender();
        }
        mPreviousX = x;
        mPreviousY = y;
        
        queueEvent(new Runnable(){
            public void run() {
                mRenderer.setColor(e.getX() / getWidth(), e.getY() / getHeight(), 1.0f);
            }});
    
        return true;
    }

    public EllipseRenderer getRenderer() {
    	return mRenderer;
    }
    
    private final float TOUCH_SCALE_FACTOR = 180.0f / 320;
    private final float TRACKBALL_SCALE_FACTOR = 36.0f;
    private EllipseRenderer mRenderer;
    private float mPreviousX;
    private float mPreviousY;
}